package com.sfx.service.impl;

import com.sfx.domain.ResponseResult;
import com.sfx.domain.vo.UserInfoVo;
import com.sfx.enums.AppHttpCodeEnum;
import com.sfx.exception.SystemException;
import com.sfx.utils.BeanCopyUtils;
import com.sfx.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sfx.domain.entity.User;
import com.sfx.mapper.UserMapper;
import com.sfx.service.UserService;

import java.util.ArrayList;

/**
* Created with IntelliJ IDEA.
* Description:
* User: sfx
* Date: 2023-07-11
* Time: 12:05
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Override
    public ResponseResult getUserInfo() {
        //1.得到当前用户ID
        Long userId = SecurityUtils.getUserId();
        //2.查询数据库
        User user = getById(userId);
        if(user == null) {
            throw new SystemException(AppHttpCodeEnum.USER_NO_EXIST);
        }
        //4.封装VO返回
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        //3.返回给前端
        return ResponseResult.okResult(userInfoVo);
    }

    @Override
    public ResponseResult updateUserInfo(User user) {
        updateById(user);
        UserInfoVo userInfoVo = BeanCopyUtils.copyBean(user, UserInfoVo.class);
        return ResponseResult.okResult(userInfoVo);
    }
}